/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myetl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author apuangr1
 */
public class Transformer {

    String table, columns;
    String[] cols;
    String[] colTypes;
    String sql, insertSql;
    AppManager apm;
    String[] Values;
    ArrayList<Function> functions = new ArrayList<Function>();

    Transformer(AppManager apm) {
        this.apm = apm;
        this.columns = this.apm.getDestColumns();
        String temp[] = columns.split(",");
        Function dummyfn = new Function();
        for (int i = 1; i <= temp.length; i++) {
            Function fx = new Function();

            String content = this.apm.get("conversion", temp[i - 1].replace(" ", ""));
            if (content == null) {
                continue;
            }

            fx.setOutputColumn(temp[i - 1].trim());
            setFunction(content, fx);
            functions.add(fx);
        }
    }

    private void setFunction(String get, Function fn) {
        String function;
        String column;
        String arguments;
        String[] temp;
        if (get == null) {
            function = "";
            column = "";
            arguments = "";

        } else {
            temp = get.split(" ", 3);
            function = temp[0];
            try {
                column = temp[1];
            } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                column = "";
            }
            try {
                arguments = temp[2];
            } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                arguments = "";
            }
        }
        fn.setInputColumn(column);
        fn.setName(function);

    }
//    public void convert(String column){
//        this.destData.
//    }

    public void listFunction() {
        ListIterator<Function> li = functions.listIterator();
        while (li.hasNext()) {
            Function fn = li.next();
            System.out.println(fn.getName() + ":" + fn.getOutputColumn());
        }
    }

    public String getFunction(String column) {
        ListIterator<Function> li = functions.listIterator();
        while (li.hasNext()) {
            Function fx = li.next();
            if (fx.getOutputColumn().equals(column)) {
                return fx.getName();
            }
        }
        return "";
    }

    public java.sql.Date convertToDate(String column, int value) {
        String function = getFunction(column);
//        if (function.contains("AS400DATE")) {
        System.out.println("+++++++ convert to ddate");
        try {
            Method mc = Command.class.getDeclaredMethod(function, new Class[]{int.class});
            java.sql.Date dd = (Date) mc.invoke(mc, value);
            return dd;
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
        } catch (SecurityException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        }
//            return Command.AS400DATE(value);
//        }
        return null;
    }

    public java.sql.Date convertToDate(String column, double value) {
        String function = getFunction(column);
//        if (function.contains("AS400DATE")) {
        System.out.println("+++++++ convert to ddate");
        try {
            Method mc = Command.class.getDeclaredMethod(function.toUpperCase(), new Class[]{double.class});
            java.sql.Date dd = (Date) mc.invoke(mc, value);
            return dd;
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
        } catch (SecurityException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        }
//            return Command.AS400DATE(value);
//        }
        return null;
    }

    public java.sql.Date convertToDate(String column, String value) {
        String function = getFunction(column);
//        if  (function.contains("AS400DATE")){
//            System.out.println("+++++++ convert to ddate");
//            return Command.AS400DATE(value);
//        }
        return null;
    }

    public java.sql.Date convertToDate(String column, java.sql.Date value) {
//        String function = getFunction(column);
//        if  (function.contains("AS400DATE")){
//            System.out.println("+++++++ convert to ddate");
//            return Command.AS400DATE(value);
//        }
        return value;
    }

    public String convertToString(String column, String value) {
        String function = getFunction(column).toUpperCase();
        if (function.length() == 0) {
            return value;
        }

//        if (function.contains("AS400DATE")) {
        System.out.println("+++++++ convert double to string");
        try {
            Method mc = Command.class.getDeclaredMethod(function, new Class[]{String.class});
            String dd = (String) mc.invoke(mc, value);
            return dd;
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
        } catch (SecurityException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        }
//            return Command.AS400DATE(value);
//        }
        return value;
    }

    public String convertToString(String column, double value) {
        String function = getFunction(column).toUpperCase();

//        if (function.contains("AS400DATE")) {
        System.out.println("+++++++ convert double to string");
        try {
            Method mc = Command.class.getDeclaredMethod(function, new Class[]{double.class});
            String dd = (String) mc.invoke(mc, value);
            return dd;
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
        } catch (SecurityException ex) {
            Logger.getLogger(Transformer.class.getName()).log(Level.SEVERE, null, ex);
        }
//            return Command.AS400DATE(value);
//        }
        return null;
    }
}
